package com.leyou.item.service.impl;

import com.leyou.item.entity.Category;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.mapper.CategoryMapper;
import com.leyou.item.service.ICategoryService;
import com.leyou.test.enums.ExceptionEnum;
import com.leyou.test.exceptions.LyException;
import com.leyou.utils.BeanHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class CategoryServiceImpl implements ICategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public List<CategoryDTO> queryListByParent(Long pid) {
        Category category = new Category();
        category.setParentId(pid);
        //根据Category对象中的非空信息查询
        List<Category> list = categoryMapper.select(category);
        if (null == list || 0 == list.size()) {
            //如果查询数据为空,抛出category为空的自定义异常枚举信息
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        //将category信息复制到categoryDTO中
        List<CategoryDTO> result = BeanHelper.copyWithCollection(list, CategoryDTO.class);
        return result;
    }

    /**
     * 根据ids查询category分类
     * @param categoryIds
     * @return
     */
    @Override
    public List<CategoryDTO> queryCategoryByIds(List<Long> categoryIds) {
        List<Category> categories = categoryMapper.selectByIdList(categoryIds);
        if (CollectionUtils.isEmpty(categories)) {
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(categories, CategoryDTO.class);
    }
}
